const CryptoJS = require('crypto-js')

export const aes_encrypt = (data, key="wojnF5WMXfmxSYls", iv="xzYFodeJs0nIQ7Pa") => {
    key = CryptoJS.enc.Utf8.parse(key)
    iv = CryptoJS.enc.Utf8.parse(iv)
    const encrypted = CryptoJS.AES.encrypt(data, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    })
    return encrypted.toString()
}

export const aes_decrypt = (data, key="wojnF5WMXfmxSYls", iv="xzYFodeJs0nIQ7Pa") => {

    // 将密钥和初始化向量转换为适合 CryptoJS 使用的格式
    key = CryptoJS.enc.Utf8.parse("wojnF5WMXfmxSYls");
    iv = CryptoJS.enc.Utf8.parse("xzYFodeJs0nIQ7Pa");
    // 执行解密操作
    const decrypted = CryptoJS.AES.decrypt(
      {
        ciphertext: CryptoJS.enc.Base64.parse(data.replace(/[\r\n]/g, ""))
      },
      key,
      {
        iv: iv,
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.CBC
      }
    );
    const plaintext = decrypted.toString(CryptoJS.enc.Utf8);
    return plaintext
}
